<script>
import { useCategoryStore } from '@/store/modules/category';
import { storeToRefs } from 'pinia';
export default {
  name: 'AppHeaderNav',
  setup() {
    const categoryStore = useCategoryStore();
    const {list} = storeToRefs(categoryStore);
    const show = (item) =>{
      categoryStore.show(item)
    }
    const hide = (item) =>{
      categoryStore.hide(item)
    }
    return {
      categoryStore,
      list,
      show,
      hide,
    }
  }
}
</script>

<template>
    <ul class="app-header-nav">
        <li class="home"><router-link :to="{
            path: '/',
        }">首页</router-link></li>
        <li v-for="item in list" :key="item.id"  @mousemove="show(item)" @mouseleave="hide(item)">
          <router-link :to="`/category/${item.id}`" @click="hide(item)">{{ item.name }}</router-link>
          <div class="layer" :class="{open:item.open}">
            <ul>
              <li v-for="sub in item.children" :key="sub.id">
                <router-link :to="`/category/sub/${sub.id}`" @click="hide(item)">
                  <img :src="sub.picture" alt="">
                  <p>{{sub.name}}</p>
                </router-link>
              </li>
            </ul>
          </div>
        </li>
    </ul>
</template>

<style scoped lang="less">
.app-header-nav {
  width: 820px;
  display: flex;
  padding-left: 40px;
  position: relative;
  z-index: 998;
  li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
    a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }
    &:hover {
        /*选择直接子元素a*/
      >a {
        color: @xtxColor;
        border-bottom: 1px solid @xtxColor;
      }
    }
  }
}
.layer {
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  /*停顿0.1s后，经过0.2显示完过渡效果*/
  transition: all .2s .1s;
  &.open {
    height: 132px;
    opacity: 1;
  }
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 60px;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>